package com.ssyt.hiring.talent.serlvet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.talent.dao.TalentDao;
import com.ssyt.hiring.talent.entity.TalentEntity;

public class TalentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final TalentDao talentDao = new TalentDao();

	public TalentServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("register".equals(command)) {
			register(request, response);
		} else if ("toLoginUI".equals(command)) {
			toLoginUI(request, response);
		} else if ("toRegisterUI".equals(command)) {
			toRegisterUI(request, response);
		} else if ("toDatePwdUI".equals(command)) {
			toDatePwdUI(request, response);
		} else if ("login".equals(command)) {
			login(request, response);
		} else if ("updatePwd".equals(command)) {
			updatepwd(request, response);
		} else if ("toUpdateUser".equals(command)) {
			// toUpdateUser(request, response);
			list(request, response);
		} else if ("updateUser".equals(command)) {
			updateUser(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 跳转至登陆页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void toLoginUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/talent/talent-login.jsp")
				.forward(request, response);
	}

	/**
	 * 跳转注册页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void toRegisterUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher(
				"/WEB-INF/views/talent/talent-register.jsp").forward(request,
				response);
	}

	/**
	 * 跳转修改密码页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void toDatePwdUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher(
				"/WEB-INF/views/talent/talent-updatepwd.jsp").forward(request,
				response);
	}

	/**
	 * 跳转修改个人信息页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	// private void toUpdateUser(HttpServletRequest request,
	// HttpServletResponse response) throws ServletException, IOException {
	// request.getRequestDispatcher(
	// "/WEB-INF/views/talent/talent-updateuser.jsp").forward(request,
	// response);
	// }

	/**
	 * 求职者注册
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取前台传过来的参数

		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int gender = Integer.parseInt(request.getParameter("gender"));
		StringBuffer sb = new StringBuffer();
		String[] hobbys = request.getParameterValues("hobby");
		for (int i = 0; i < hobbys.length; i++) {
			if (i == hobbys.length - 1) {
				sb.append(hobbys[i]);
			} else {
				sb.append(hobbys[i] + "|");
			}
		}

		// 调用业务逻辑
		TalentEntity talentEntity = new TalentEntity();
		talentEntity.setCode(code);
		talentEntity.setPassword(password);
		talentEntity.setName(name);
		talentEntity.setPhone(phone);
		talentEntity.setEmail(email);
		talentEntity.setAge(age);
		talentEntity.setGender(gender);
		talentEntity.setHobby(sb.toString());
		talentDao.resiter(talentEntity);
		// 转向
		request.getRequestDispatcher("/WEB-INF/views/talent/talent-jump.jsp")
				.forward(request, response);
	}

	/**
	 * 求职者登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void login(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取前台传过来的参数
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		// 调用业务逻辑
		TalentEntity talent = talentDao.login(code, password);
		// 转向
		if (talent == null) {
			request.setAttribute("msg", "用户名或密码错误！");
			request.getRequestDispatcher(
					"/WEB-INF/views/talent/talent-login.jsp").forward(request,
					response);
		} else {
			request.getSession().setAttribute("talent", talent);
			// request.getSession().setAttribute("name", code);
			response.sendRedirect("ResumeServlet?command=list&pageNo=1&pageSize=10");
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updatepwd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取前台传过来的参数
		TalentEntity talentEntity = (TalentEntity) request.getSession()
				.getAttribute("talent");
		if (talentEntity != null) {
			int id = talentEntity.getId();
			String pwd = request.getParameter("password");
			String pwd1 = request.getParameter("password1");
			String pwd2 = request.getParameter("password2");
			// 调用业务逻辑
			if (pwd1.equals(pwd2)) {
				int row = talentDao.updatepwd(id, pwd1, pwd);
				if (row != 0) {
					request.setAttribute("msg", "修改成功，请重新登录！");
					request.getRequestDispatcher(
							"/WEB-INF/views/talent/talent-login.jsp").forward(
							request, response);
				} else {
					request.setAttribute("msg", "原密码不正确，修改失败！");
					request.getRequestDispatcher(
							"/WEB-INF/views/talent/talent-updatepwd.jsp")
							.forward(request, response);
				}
			} else {
				request.setAttribute("msg", "修改失败，两次密码不同！");
				request.getRequestDispatcher(
						"/WEB-INF/views/talent/talent-updatepwd.jsp").forward(
						request, response);
			}
		}
		// 转向
	}

	/**
	 * 求职者修改个人信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updateUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取前台传过来的参数
		TalentEntity talentEntity = (TalentEntity) request.getSession()
				.getAttribute("talent");
		if (talentEntity != null) {
			int id = talentEntity.getId();
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			int age = Integer.parseInt(request.getParameter("age"));
			int gender = Integer.parseInt(request.getParameter("gender"));
			StringBuffer sb = new StringBuffer();
			String[] hobbys = request.getParameterValues("hobby");
			for (int i = 0; i < hobbys.length; i++) {
				if (i == hobbys.length - 1) {
					sb.append(hobbys[i]);
				} else {
					sb.append(hobbys[i] + "|");
				}
			}
			// 调用业务逻
			if (name != null || phone != null || email != null || age != 0
					|| gender != 0 || hobbys != null) {
				talentEntity.setId(id);
				talentEntity.setName(name);
				talentEntity.setPhone(phone);
				talentEntity.setEmail(email);
				talentEntity.setAge(age);
				talentEntity.setGender(gender);
				talentEntity.setHobby(sb.toString());
				talentDao.updateuser(talentEntity);
				// 转向
				request.getRequestDispatcher(
						"/WEB-INF/views/talent/talent-main.jsp").forward(
						request, response);
			}

		}
	}

	/**
	 * 注销
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取前台传过来的参数
		// 调用业务逻辑
		request.getSession().invalidate();
		// 3、转向
		response.sendRedirect("index.jsp");
	}

	/**
	 * 查询个人资料
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		TalentEntity talentEntity = (TalentEntity) request.getSession()
				.getAttribute("talent");
		if (talentEntity != null) {
			int id = talentEntity.getId();
			talentEntity.setId(id);
			TalentEntity talentList = talentDao.list(id);
			if (talentList != null) {
				request.setAttribute("talenList", talentList);
				request.getRequestDispatcher(
						"/WEB-INF/views/talent/talent-updateuser.jsp").forward(
						request, response);
			}
		}
	}

}
